package com.uroad.lib.widget.dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TimePicker;

import com.uroad.lib.R;

public class TimeDialog extends Dialog {

    Context mContext;
    Button btnCancel, btnOK;
    TimePicker timePicker;
    DialogOnclick dialogOkClick = null;

    public TimeDialog(Context context, final DialogOnclick dialogOkClick) {
        super(context, R.style.baseCustomDialog);
        mContext = context;
        this.dialogOkClick = dialogOkClick;
        View view = LayoutInflater.from(mContext).inflate(
                R.layout.dialog_time, null);
        timePicker = (TimePicker) view.findViewById(R.id.timePicker);
        //timePicker.setIs24HourView(true);
        btnCancel = (Button) view.findViewById(R.id.btnCancel);
        btnOK = (Button) view.findViewById(R.id.btnOK);
        btnOK.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (dialogOkClick != null) {
                    String hour = timePicker.getCurrentHour()+"";
                    String minute = timePicker.getCurrentMinute()+"";
                    if(timePicker.getCurrentHour() < 10){
                        hour = "0"+timePicker.getCurrentHour();
                    }
                    if(timePicker.getCurrentMinute() < 10){
                        minute = "0"+timePicker.getCurrentMinute();
                    }
                    dialogOkClick.PerDialogclick(TimeDialog.this,
                            hour + ":" + minute+":00");
                } else {
                    hideDialog();
                }
            }
        });
        btnCancel.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                hideDialog();
            }
        });
        setCanceledOnTouchOutside(false);
        setContentView(view);
    }

    public void hideDialog() {
        this.dismiss();
    }

    public interface DialogOnclick {
        void PerDialogclick(Dialog dialog, String time);
    }
}
